<?php
namespace Tongcheng\Controller;
use Think\Controller;

/**
 * Class UnuseBackController
 * @package Tongcheng\Controller
 * 闲置管理
 */
class UnuseBackController extends CommonController {
    /**
     * 获取闲置列表
     */
    public function index(){
        $model = M("Unuse");
        $map = array();
//        $map['wid'] = session('wid');
//        $map['status'] = 1;
        // $map['is_pay'] = 1;

        $res['count'] = $model->where($map)->count();
        $page = $_GET['page']?$_GET['page']:1;
        $limit = $_GET['limit']?$_GET['limit']:10;
        $list = $model->field("tp_unuse.*,s.nickname,FROM_UNIXTIME(tp_unuse.add_time,\"%m-%d %H:%i\") time")->join("tp_member s on s.uid = tp_unuse.uid",'left')->where()->page("{$page},{$limit}")->order('id desc')->select();
        $res['list']= $list;
//        $res['count']= $list;
        if(IS_AJAX){
            $this->ajaxReturn($res);
        }else{
            $this->assign('res', $res);
            $this->display();
        }
    }
    /**
     * 推荐闲置
     */
    public function tuijian(){
        $id = I('id');
        $unuse = M('Unuse')->where('id = '.$id)->select();
        if(!empty($unuse[0])){
            $result = M('Unuse')->where('id = '.$id)->save(array('is_tuijian'=>($unuse[0]['is_tuijian']==1?0:1)));
            if($result){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }else{
            $this->ajaxReturn(0);
        }
    }

    /**
     * 查看闲置详情
     */
    public function edit(){
        $booksM = M("Unuse");
        $id = $_GET['id'];
        $list = $booksM->field('tp_unuse.*, tp_member.*,FROM_UNIXTIME(tp_unuse.add_time,"%m-%d %H:%i") time')->join('tp_member on tp_unuse.uid = tp_member.uid','left')->where(['tp_unuse.id' => $id])->find();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 删除闲置
     */
    public function del(){
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(0);
        }
        $result = M('Unuse')->where('id = '.$id)->delete();
        if($result){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }
}